package com.yanxx.codegenerator.controller;

import com.yanxx.codegenerator.service.CodeGeneratorService;
import com.yanxx.codegenerator.model.CodeGenerateResponse;
import com.yanxx.codegenerator.model.CodeOperationResponse;
import com.yanxx.codegenerator.model.CodeStatusResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.http.ResponseEntity;

/**
 * 编码生成器REST API控制器
 */
@Tag(name = "编码生成器API", description = "提供业务编码的生成、确认、回收等功能")
@RestController
@RequestMapping("/api/codes")
public class CodeGeneratorController {

    private final CodeGeneratorService codeGeneratorService;

    public CodeGeneratorController(CodeGeneratorService codeGeneratorService) {
        this.codeGeneratorService = codeGeneratorService;
    }

    /**
     * 生成新编码
     */
    @Operation(summary = "生成新编码", description = "生成一个新的业务编码，状态为待确认")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功生成编码"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/generate")
    public ResponseEntity<CodeGenerateResponse> generateCode() {
        String code = codeGeneratorService.generateCode();
        return ResponseEntity.ok(new CodeGenerateResponse(code));
    }

    /**
     * 确认编码已使用
     */
    @Operation(summary = "确认编码已使用", description = "确认编码已被业务使用，将状态从待确认改为已确认")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "确认操作完成"),
            @ApiResponse(responseCode = "400", description = "编码不存在或状态不正确"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/confirm/{code}")
    public ResponseEntity<CodeOperationResponse> confirmCode(
            @Parameter(description = "要确认的编码", required = true, example = "CODE000001") @PathVariable String code) {
        boolean confirmed = codeGeneratorService.confirmCode(code);
        return ResponseEntity.ok(new CodeOperationResponse(confirmed, confirmed ? "编码确认成功" : "编码确认失败"));
    }

    /**
     * 回收未使用的编码
     */
    @Operation(summary = "回收未使用的编码", description = "回收未使用的编码，使其可以被重新分配")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "回收操作完成"),
            @ApiResponse(responseCode = "400", description = "编码不存在或已被确认使用"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/recycle/{code}")
    public ResponseEntity<CodeOperationResponse> recycleCode(
            @Parameter(description = "要回收的编码", required = true, example = "CODE000001") @PathVariable String code) {
        boolean recycled = codeGeneratorService.recycleCode(code);
        return ResponseEntity.ok(new CodeOperationResponse(recycled, recycled ? "编码回收成功" : "编码回收失败"));
    }

    /**
     * 获取可用的回收编码数量
     */
    @Operation(summary = "获取可用的回收编码数量", description = "获取当前可用于重新分配的回收编码数量")
    @ApiResponse(responseCode = "200", description = "成功获取数量")
    @GetMapping("/available-count")
    public ResponseEntity<Map<String, Integer>> getAvailableCount() {
        int count = codeGeneratorService.getAvailableCount();
        Map<String, Integer> response = new HashMap<>();
        response.put("count", count);
        return ResponseEntity.ok(response);
    }

    /**
     * 获取所有待确认的编码
     */
    @Operation(summary = "获取所有待确认的编码", description = "获取所有已生成但尚未确认使用的编码列表")
    @ApiResponse(responseCode = "200", description = "成功获取待确认编码列表")
    @GetMapping("/pending")
    public ResponseEntity<Set<String>> getPendingCodes() {
        return ResponseEntity.ok(codeGeneratorService.getPendingCodes());
    }

    /**
     * 获取所有已确认的编码
     */
    @Operation(summary = "获取所有已确认的编码", description = "获取所有已确认使用的编码列表")
    @ApiResponse(responseCode = "200", description = "成功获取已确认编码列表")
    @GetMapping("/confirmed")
    public ResponseEntity<Set<String>> getConfirmedCodes() {
        return ResponseEntity.ok(codeGeneratorService.getConfirmedCodes());
    }

    /**
     * 获取所有已回收的编码
     */
    @Operation(summary = "获取所有已回收的编码", description = "获取所有已回收可重新分配的编码列表")
    @ApiResponse(responseCode = "200", description = "成功获取已回收编码列表")
    @GetMapping("/recycled")
    public ResponseEntity<Set<String>> getRecycledCodes() {
        return ResponseEntity.ok(codeGeneratorService.getRecycledCodes());
    }

    /**
     * 获取编码生成器状态信息
     */
    @Operation(summary = "获取编码生成器状态信息", description = "获取编码生成器的完整状态统计信息")
    @ApiResponse(responseCode = "200", description = "成功获取状态信息")
    @GetMapping("/status")
    public ResponseEntity<CodeStatusResponse> getStatus() {
        CodeStatusResponse status = new CodeStatusResponse(
                codeGeneratorService.getAvailableCount(),
                codeGeneratorService.getPendingCodes().size(),
                codeGeneratorService.getConfirmedCodes().size(),
                codeGeneratorService.getRecycledCodes().size());
        return ResponseEntity.ok(status);
    }
}